package com.king.tv.mvp.presenter;

import android.text.TextUtils;
import android.util.Log;
import android.widget.TextView;

import com.king.base.util.LogUtils;
import com.king.tv.App;
import com.king.tv.bean.Room;
import com.king.tv.bean.RoomLine;
import com.king.tv.http.BaseCallModel;
import com.king.tv.http.HttpLoader;
import com.king.tv.mvp.base.BasePresenter;
import com.king.tv.mvp.view.IRoomView;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;


/**
 * @author Jenly <a href="mailto:jenly1314@gmail.com">Jenly</a>
 * @since 2017/3/7
 */

public class RoomPresenter extends BasePresenter<IRoomView> {

    public RoomPresenter(App app) {
        super(app);
    }

    public void enterRoom(String uid){
        enterRoom(uid,false);
    }

    public void enterRoom(String uid,final boolean isShowing){
        if(isViewAttached())
            getView().showProgress();
        HttpLoader.getInstance().getMovieurl(uid).subscribe(new Observer<BaseCallModel<String>>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseCallModel<String> value) {
                Log.i("tag","===========value"+value.getData());
                if(isViewAttached())
                    if(!TextUtils.isEmpty(value.getData()))
                    getView().playUrl(value.getData());
//                    getView().showProgress();
            }

            @Override
            public void onError(Throwable e) {
                if(isViewAttached())
                    getView().onError(e);
            }

            @Override
            public void onComplete() {
                if(isViewAttached())
                    getView().onCompleted();
            }
        });
        getAppComponent().getAPIService()
                .enterRoom(uid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Room>() {
                    @Override
                    public void onError(Throwable e) {
                        if(isViewAttached())
                            getView().onError(e);
                    }

                    @Override
                    public void onComplete() {
                        if(isViewAttached())
                            getView().onCompleted();
                    }

                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Room room) {
                        LogUtils.d("Response:" + room);
                        if(isViewAttached())
                        getView().enterRoom(room);

                        if(room!= null){
                            String url =null;
//                            RoomLine roomLine = room.getRoom_lines().get(0);
                            RoomLine roomLine = room.getLive().getWs();

                            RoomLine.FlvBean flv = roomLine.getFlv();
                            LogUtils.d("flv:" + flv);
                            if(flv!=null){
                                url = flv.getValue(isShowing).getSrc();
                            }else{
                                url = roomLine.getHls().getValue(isShowing).getSrc();
                            }
                            if(isViewAttached())
                                getView().playUrl(url);
                        }

                    }
                });


    }



}
